
package pe.com.bn.sach.mantenimiento.controller;

import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import pe.com.bn.sach.seguridad.DatosSesion;
import pe.com.bn.sach.service.MantenimientoService;
import pe.com.bn.sach.service.SelectService;
import pe.com.bn.sach.mantenimiento.form.PromotorForm;
import pe.com.bn.sach.common.Constante;
import pe.com.bn.sach.domain.Bnchf07Item;
import pe.com.bn.sach.domain.Bnchf07ItemId;
import pe.com.bn.sach.domain.Bnchf08Ubigeo;
import pe.com.bn.sach.domain.Bnchf37Promotor;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Hashtable;

import pe.com.bn.sach.common.Util;

public class PromotorController extends MultiActionController {
	
    private String listar;
    Bnchf37Promotor bnchf37Promotor = new Bnchf37Promotor();
    Bnchf37Promotor Bnchf37Promotor = new Bnchf37Promotor();
    
    private MantenimientoService servicioMantenimiento;
    private SelectService servicioSelect;
    
    private String commandName;
    private Class commandClass;
    
    
    /** Logger for this class and subclasses */    
    protected final Log logger = LogFactory.getLog(getClass());
    
    public PromotorController()
    {
        ParameterMethodNameResolver resolver = new ParameterMethodNameResolver();
        resolver.setParamName("method");
        resolver.setDefaultMethodName("cargaListaPromotor");
        setMethodNameResolver(resolver);
    }


    
    public ModelAndView verificarDocumentoPromotor(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	
    	
    	boolean rptaService=false;
    	
		ModelAndView mav = new ModelAndView("promotor/rep_RegistrarPromotorValida");
	try {
		Hashtable ht=new Hashtable();
		
			
		ht.put("Tabla",(String)"37");
		
		ht.put("TipoDocumento",(Long)(Util.copyLongc(""+request.getParameter("TipoDocumento"))));
		ht.put("NroDocumento",(String)(request.getParameter("NroDocumento")));		
		
		rptaService=servicioMantenimiento.validacionDeTiposDocumentos(ht);
		
		if(rptaService){
			mav.addObject("txtRpta","true");	
		}else 
			mav.addObject("txtRpta","false");
		
		
		mav=select(mav);
		
	} catch (Exception e) {
		// TODO: handle exception
	}	
		return mav;
    }
    
    
    
    // INICIO MANTENIMIENTO    
    public Bnchf37Promotor formBuscarInit(HttpServletRequest request) {
    	
    	Bnchf37Promotor bnchf37Promotor=new Bnchf37Promotor();
    	bnchf37Promotor.setF37IdPromotor(Util.copyLongc("0"));
    	bnchf37Promotor.setF37Sexo(null);
    	bnchf37Promotor.setF37EstdCivil(null);
    	bnchf37Promotor.setF37TpoDoc(null);
    	bnchf37Promotor.setF37NumDoc(null);
    	bnchf37Promotor.setF37ApePat(null);
    	bnchf37Promotor.setF37ApeMat(null);
    	bnchf37Promotor.setF37Nomb(null);  	
 	
    	return bnchf37Promotor;
    }
    /* busca promotor ini */
    public Bnchf37Promotor formBuscar(HttpServletRequest request) {
    	
    
    	Bnchf37Promotor bnchf37Promotor=new Bnchf37Promotor();
    	bnchf37Promotor.setF37IdPromotor(Util.copyLongc(request.getParameter("txtCodigoPromotor")));
    	bnchf37Promotor.setF37TpoDoc(Util.copyLong(""+request.getParameter("selNroDocumento")));
    	bnchf37Promotor.setF37NumDoc(""+request.getParameter("txtNroDocumento"));
    	bnchf37Promotor.setF37ApePat(""+request.getParameter("txtApePaterno"));
    	bnchf37Promotor.setF37ApeMat(""+request.getParameter("txtApeMaterno"));
    	bnchf37Promotor.setF37Nomb(""+request.getParameter("txtNombres"));
    	return bnchf37Promotor;
    }
    /* busca promotor ini */
    public ModelAndView cargaListaPromotor(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	
    	
    	
		ModelAndView mav = new ModelAndView("mantenimiento/promotor/rep_BuscarPromotor");
		
		
		bnchf37Promotor=formBuscarInit(request);
		mav.addObject("listarPromotor",servicioMantenimiento.listarPromotor(bnchf37Promotor));
		Bnchf07Item bnchf07Item=new Bnchf07Item();
  		bnchf07Item.getId().getBnchf06Tabla().setF06IdTabla(Util.copyLong("4")); //doc
  		mav.addObject("ListTipoDocumento",servicioSelect.listItem(bnchf07Item));
  		
		return mav;
    }

    
    public ModelAndView cargaListaPromotorAjax(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
 		ModelAndView mav = new ModelAndView("mantenimiento/promotor/rep_BuscarPromotorAjax");
		//optSeleccion
		try {
			bnchf37Promotor=formBuscar(request);
			mav.addObject("listarPromotor",servicioMantenimiento.listarPromotor(bnchf37Promotor));	
		} catch (Exception e) {
			bnchf37Promotor=formBuscarInit(request);
			mav.addObject("listarPromotor",servicioMantenimiento.listarPromotor(bnchf37Promotor));
		}
		
		
		return mav;
    }
    
    public ModelAndView nuevo(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	
		ModelAndView mav = new ModelAndView("mantenimiento/promotor/rep_RegistrarPromotor");
		mav=select(mav);
		return mav;
    }

    public ModelAndView select(ModelAndView mav)
    throws Exception   
    {	
    	Bnchf07Item bnchf07Item=new Bnchf07Item();
    	mav.addObject("ListDepartamento",servicioSelect.listUbigeoDepartamento());
    	
  		bnchf07Item.getId().getBnchf06Tabla().setF06IdTabla(Util.copyLong("4")); // 02 sexo
  		mav.addObject("ListTipoDocumento",servicioSelect.listItem(bnchf07Item));    	  
    	bnchf07Item.getId().getBnchf06Tabla().setF06IdTabla(Util.copyLong("7")); // Inmueble    	
  		mav.addObject("ListZona",servicioSelect.listItem(bnchf07Item));
  		bnchf07Item.getId().getBnchf06Tabla().setF06IdTabla(Util.copyLong("21")); // Inmueble
  		mav.addObject("ListVia",servicioSelect.listItem(bnchf07Item));
  		bnchf07Item.getId().getBnchf06Tabla().setF06IdTabla(Util.copyLong("22")); // Inmueble
  		mav.addObject("ListExterior",servicioSelect.listItem(bnchf07Item));
  		bnchf07Item.getId().getBnchf06Tabla().setF06IdTabla(Util.copyLong("23")); // Inmueble
  		mav.addObject("ListInterior",servicioSelect.listItem(bnchf07Item));
  		bnchf07Item.getId().getBnchf06Tabla().setF06IdTabla(Util.copyLong("7")); // ubicacion
  		mav.addObject("ListUbicacion",servicioSelect.listItem(bnchf07Item));  		
  		bnchf07Item.getId().getBnchf06Tabla().setF06IdTabla(Util.copyLong("33")); // ListEstado
  		mav.addObject("ListEstado",servicioSelect.listItem(bnchf07Item));
		bnchf07Item.getId().getBnchf06Tabla().setF06IdTabla(copyLong("1")); // 01 estado civil
 		mav.addObject("ListEstadoCivil",servicioSelect.listItem(bnchf07Item)); 		
 		bnchf07Item.getId().getBnchf06Tabla().setF06IdTabla(copyLong("2")); // 02 sexo
 		mav.addObject("ListSexo",servicioSelect.listItem(bnchf07Item)); 		
  		// ListEstado
  		
    	return mav;
    }
    
    public ModelAndView encontrarPromotor(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {    	 
    	ModelAndView mav = new ModelAndView();
     	if(request.getParameter("txtOpcion").equals("bajar"))  	
    	mav = new ModelAndView("mantenimiento/promotor/rep_BajaPromotor"); // contular-modificar-bajar
    	if(request.getParameter("txtOpcion").equals("consultar"))
    	mav = new ModelAndView("mantenimiento/promotor/rep_ConsultaPromotor"); // contular-modificar-bajar
    	if(request.getParameter("txtOpcion").equals("modificar"))
    	mav = new ModelAndView("mantenimiento/promotor/rep_ModificarPromotor"); // contular-modificar-bajar
    	Bnchf37Promotor bnchf37Promotor=new Bnchf37Promotor();
    	bnchf37Promotor.setF37IdPromotor(Util.copyLong(""+request.getParameter("optSeleccion")));
    
    	Bnchf08Ubigeo bnchf08Ubigeo=new Bnchf08Ubigeo();
    	
    	bnchf37Promotor=servicioMantenimiento.encontrarPromotorMante(bnchf37Promotor);
    
        try {bnchf08Ubigeo.setF08Cubigeo(bnchf37Promotor.getF37DptoDirecc()+bnchf37Promotor.getF37ProvDirecc()+bnchf37Promotor.getF37DistriDirecc());} catch (Exception e) {logger.error("Error "+e);	}
        bnchf08Ubigeo=servicioSelect.encontrarDistrito(bnchf08Ubigeo);

        Bnchf08Ubigeo provincia=new Bnchf08Ubigeo();       
        provincia.getId().setF08IdDpto(bnchf37Promotor.getF37DptoDirecc());
        bnchf08Ubigeo=servicioSelect.encontrarDistrito(bnchf08Ubigeo);        
  		mav.addObject("ListProvinciaUbigeo",servicioSelect.listProvincia(provincia));  		
  		Bnchf08Ubigeo distrito=new Bnchf08Ubigeo();
  		distrito.getId().setF08IdDpto(bnchf37Promotor.getF37DptoDirecc());
  		distrito.getId().setF08IdProv(bnchf37Promotor.getF37ProvDirecc());
  		mav.addObject("ListDistritoUbigeo",servicioSelect.listDistrito(distrito));  		
  		bnchf08Ubigeo.getId().setF08IdProv(bnchf37Promotor.getF37ProvDirecc());
  		bnchf08Ubigeo.getId().setF08IdDistri(bnchf37Promotor.getF37DistriDirecc());
  		
  		mav.addObject("Ubigeo",bnchf08Ubigeo);
  		
  		
     	mav.addObject("Promotor",bnchf37Promotor);
    	mav=select(mav); 
    	return mav;
    }
      
    Bnchf37Promotor getForm(PromotorForm promotorForm){
    
    	Bnchf37Promotor bnchf37Promotor=new Bnchf37Promotor();
    	//F39_ID_USUA_CREA txtIdPromotor
    	/*selSexo*/
    	bnchf37Promotor.setF37IdPromotor(Util.copyLong(""+promotorForm.getTxtIdPromotor()));
    	bnchf37Promotor.setF37Sexo(Util.copyLongc(""+promotorForm.getSelSexo()));
    	bnchf37Promotor.setF37EstdCivil(Util.copyLongc(""+promotorForm.getSelEstadoCivil()));
    	bnchf37Promotor.setF37TpoDoc(Util.copyLong(promotorForm.getSelNroDocumento()));
    	bnchf37Promotor.setF37NumDoc(promotorForm.getTxtNroDocumento());
    	bnchf37Promotor.setF37ApePat(promotorForm.getTxtApePaterno());
    	bnchf37Promotor.setF37ApeMat(promotorForm.getTxtApeMaterno());
    	bnchf37Promotor.setF37Nomb(promotorForm.getTxtNombres());
    	bnchf37Promotor.setF37CtaCorreo(promotorForm.getTxtCorreo());
    	bnchf37Promotor.setF37TelfFijo(promotorForm.getTxtTelefijo());
    	bnchf37Promotor.setF37TelfCel(promotorForm.getTxtCelular());
    	
    	if(promotorForm.getSelDepartamentoDato()!=null)
    	if(promotorForm.getSelDepartamentoDato().length()==4)
    	bnchf37Promotor.setF37DptoDirecc("0");
    	else
    	bnchf37Promotor.setF37DptoDirecc(promotorForm.getSelDepartamentoDato());
    	
    	if(promotorForm.getSelDepartamentoDato()==null || promotorForm.getSelDepartamentoDato().equals(""))
    	bnchf37Promotor.setF37DptoDirecc("0");
    	
    	
    	if(promotorForm.getSelProvinciaDato()!=null)
    	if(promotorForm.getSelProvinciaDato().length()==4)
    	bnchf37Promotor.setF37ProvDirecc("0");	
    	else
    	bnchf37Promotor.setF37ProvDirecc(promotorForm.getSelProvinciaDato());
    	
    	if(promotorForm.getSelProvinciaDato()==null||promotorForm.getSelProvinciaDato().equals(""))
    	bnchf37Promotor.setF37ProvDirecc("0");
    	
    	if(promotorForm.getSelDistritoDato()!=null )
    	if(promotorForm.getSelDistritoDato().length()==4)
    	bnchf37Promotor.setF37DistriDirecc("0");
        else	
    	bnchf37Promotor.setF37DistriDirecc(promotorForm.getSelDistritoDato());
    	
    	if(promotorForm.getSelDistritoDato()==null || promotorForm.getSelDistritoDato().equals(""))
    	bnchf37Promotor.setF37DistriDirecc("0");
    	
    	
    	bnchf37Promotor.setF37TpoZona(Util.copyLong(promotorForm.getSelZona()));
    	bnchf37Promotor.setF37NomZona(promotorForm.getTxtNomZona());
    	bnchf37Promotor.setF37TpoVia(Util.copyLong(promotorForm.getSelVia()));
    	bnchf37Promotor.setF37NomVia(promotorForm.getTxtNomVia());		    	
    	bnchf37Promotor.setF37TpoExt(Util.copyLong(promotorForm.getSelExt()));
    	bnchf37Promotor.setF37NomExt(promotorForm.getTxtNomExt());
    	bnchf37Promotor.setF37TpoInt(Util.copyLong(promotorForm.getSelInt()));
    	bnchf37Promotor.setF37NomInt(promotorForm.getTxtNomInt());
    	bnchf37Promotor.setF37Piso(promotorForm.getTxtPiso());
    	bnchf37Promotor.setF37Observ(promotorForm.getTxtObservacion());
    	bnchf37Promotor.getBnchf10Instruccion().setF10IdInstruccion(Util.copyLong(promotorForm.getTxtIdInstruccion()));
    	bnchf37Promotor.getBnchf09Profesion().setF09IdProfesion(Util.copyLong(""+promotorForm.getTxtIdProfesion()));
    	
    	try {bnchf37Promotor.setF37FeNac(Util.copyDate(""+promotorForm.getTxtFechNacimiento()));} catch (Exception e) {bnchf37Promotor.setF37FeNac(null);}
    	/*
    	 	private String txtIdInstruccion;
	private String  txtIdProfesion;
    	 * */
    	
    	
    	bnchf37Promotor.setF37TelfFijo(""+promotorForm.getTxtTelefijo());
    	bnchf37Promotor.setF37TelfCel(""+promotorForm.getTxtCelular());    	
    	
    
    	return bnchf37Promotor;
		}
    public ModelAndView guardarPromotor(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {   	
    
 		Object command = getCommandObject(request); 		
 		PromotorForm promotorForm=new PromotorForm (); 		
		promotorForm = (PromotorForm)command;		
    	ModelAndView mav = new ModelAndView("mantenimiento/promotor/rep_BuscarPromotor");
    	bnchf37Promotor=new Bnchf37Promotor();    	
    	bnchf37Promotor=getForm(promotorForm);
    	DatosSesion datosSesion=(DatosSesion)request.getSession().getAttribute("datosSesion");
    	bnchf37Promotor.setF37IdUsuaCrea(datosSesion.getCodigoHost());
    	 
    	mav.addObject("Msj",servicioMantenimiento.guardarPromotor(bnchf37Promotor));
    	return cargaListaPromotor(request,response);
    }  

    public ModelAndView modificarPromotor(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {   	
    	
 		Object command = getCommandObject(request); 		
 		PromotorForm promotorForm=new PromotorForm (); 		
		promotorForm = (PromotorForm)command;		
    	ModelAndView mav = new ModelAndView("mantenimiento/promotor/rep_BuscarPromotor");   	
    	bnchf37Promotor=new Bnchf37Promotor();    	
    	bnchf37Promotor=getForm(promotorForm);
    	DatosSesion datosSesion=(DatosSesion)request.getSession().getAttribute("datosSesion");
    	bnchf37Promotor.setF37IdUsuaCrea(datosSesion.getCodigoHost());
    	
    	mav.addObject("Msj",servicioMantenimiento.modificarPromotor(bnchf37Promotor));
		return cargaListaPromotor(request,response);
    }

    public ModelAndView bajarPromotor(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {   	
    	
 		Object command = getCommandObject(request); 		
 		PromotorForm promotorForm=new PromotorForm (); 		
		promotorForm = (PromotorForm)command;		
    	ModelAndView mav = new ModelAndView("mantenimiento/promotor/rep_BuscarPromotor");   	
    	bnchf37Promotor=new Bnchf37Promotor();  	
    	bnchf37Promotor=getForm(promotorForm);
    	mav.addObject("Msj",servicioMantenimiento.bajarPromotor(bnchf37Promotor));
    	return cargaListaPromotor(request,response);
    }
    
    
    public ModelAndView consultarReniecf(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	
		ModelAndView mav = new ModelAndView("mantenimiento/promotor/rep_ConsultarPopupReniecFrame");
		return mav;
    }
    
    public ModelAndView consultarReniec(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    
		ModelAndView mav = new ModelAndView("mantenimiento/promotor/rep_ConsultarPopupReniec");
		return mav;
    } 

    // FIN MANTENIMIENTO
    
   

   
    public ModelAndView seleccionarPromotor(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	ModelAndView mav = new ModelAndView("notaria/seleccionarPromotorFrame");
    	mav=cargarCombos(mav);
		return mav;
       
    }
    

    public ModelAndView buscarPromotor(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	
    	reiniciarPromotor();
    	PromotorForm notariaForm = new PromotorForm();
    	Object command = getCommandObject(request);
		notariaForm = (PromotorForm)command;
		  if(notariaForm.getTxtCodPromotor()!=null && !notariaForm.getTxtCodPromotor().equals("")) Bnchf37Promotor.setF37IdPromotor(copyLong(notariaForm.getTxtCodPromotor()));
		  if(notariaForm.getSelDocumentoRepre()!=null && !notariaForm.getSelDocumentoRepre().equals("")) Bnchf37Promotor.setF37TpoDoc(copyLong(notariaForm.getSelDocumentoRepre()));
		   Bnchf37Promotor.setF37ApePat(copyString(notariaForm.getTxtApePaternoRepre()));
		  Bnchf37Promotor.setF37ApeMat(copyString(notariaForm.getTxtApeMaternoRepre()));
		  Bnchf37Promotor.setF37Nomb(copyString(notariaForm.getTxtNombresRepre()));
		  if(notariaForm.getSelDocumentoRepre()!=null && !notariaForm.getSelDocumentoRepre().equals("")) Bnchf37Promotor.setF37TpoDoc(copyLong(notariaForm.getSelDocumentoRepre()));
		  
		
		 
    	ModelAndView mav = new ModelAndView("notaria/seleccionarPromotor");
    	mav=cargarCombos(mav);
    	mav.addObject("lista_notaria",servicioMantenimiento.buscarPromotor(Bnchf37Promotor));
    	reiniciarForm(notariaForm);
    	
    	return mav;
    }
    

    
    
     
    
  
    public ModelAndView cargarCombos(ModelAndView mav) throws Exception{
    	Bnchf07ItemId itemid= new Bnchf07ItemId();
		Bnchf07Item item= new Bnchf07Item();
		itemid.getBnchf06Tabla().setF06IdTabla(new Long(19));
		item.setId(itemid);
		mav.addObject("lstTipoPersona",servicioSelect.listItem(item));
		mav.addObject("lstEstado", servicioSelect.listItemEstadoCliente());
		mav.addObject("lstEstCivil", servicioSelect.listItemEstadoCivil());
		mav.addObject("lstDocumento", servicioSelect.listItemDNI());
		mav.addObject("opciondefecto",Constante.CONST_SELECT_DEFAULT);
		
    	return mav;
    }
    
    
    public void reiniciarPromotor(){
    	Bnchf37Promotor.setF37IdPromotor(null);
    	Bnchf37Promotor.setF37ApeMat("");
    	Bnchf37Promotor.setF37ApePat("");    	
    	Bnchf37Promotor.setF37Nomb("");
    	Bnchf37Promotor.setF37NumDoc("");
    }
    public void reiniciarForm(PromotorForm notariaForm){
    	notariaForm.setSelDepartamento("");
    	notariaForm.setSelDistrito("");
    	notariaForm.setSelDocumentoRepre("");
    	notariaForm.setSelProvincia("");
    	notariaForm.setTxtApePaternoRepre("");
    	notariaForm.setTxtApeMaternoRepre("");
    	notariaForm.setTxtCelular("");
    	notariaForm.setTxtCodPromotor("");
    	notariaForm.setTxtCorreo("");
    	Bnchf37Promotor.setF37Nomb("");
    }
    
 
    
    public Object getCommandObject(HttpServletRequest request)
    throws Exception
    {
    Object command = formBackingObject(request);
    bind(request, command);
    return command;
    }
    protected Object formBackingObject(HttpServletRequest request)
    throws Exception
    {
        return commandClass.newInstance();
    }


	public Class getCommandClass() {
		return commandClass;
	}


	public void setCommandClass(Class commandClass) {
		this.commandClass = commandClass;
	}


	public String getCommandName() {
		return commandName;
	}


	public void setCommandName(String commandName) {
		this.commandName = commandName;
	}

	public String copyString(String vari){
		if(vari == null || vari == "null" )
			vari="";
		return vari;
		
	}
	
	
	public int copyInt(String vari){
		if(vari == null || vari == "null" )
			vari="0";
		return Integer.parseInt(vari);
	}
	
	public Long copyLong(String vari){
		return new Long(vari);
	}
	
	public java.util.Date copyDate(String vari) throws ParseException{
		SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy");
		java.util.Date d = null;
		if(vari == null || vari == "null" )
			vari ="10-10-2000";
			d=sdf.parse(vari);
		return d;
		
	}

	
	

	/**
	 * @return Devuelve servicioSelect.
	 */
	public SelectService getServicioSelect() {
		return servicioSelect;
	}
	/**
	 * @param servicioSelect El servicioSelect a establecer.
	 */
	public void setServicioSelect(SelectService servicioSelect) {
		this.servicioSelect = servicioSelect;
	}
	
	
	/**
	 * @return Devuelve servicioMantenimiento.
	 */
	public MantenimientoService getServicioMantenimiento() {
		return servicioMantenimiento;
	}
	/**
	 * @param servicioMantenimiento El servicioMantenimiento a establecer.
	 */
	public void setServicioMantenimiento(
			MantenimientoService servicioMantenimiento) {
		this.servicioMantenimiento = servicioMantenimiento;
	}
}


